package com.job.admin.audit.listener;

import com.alibaba.fastjson2.JSON;
import com.job.admin.audit.mapper.AuditAvatarMapper;
import com.job.admin.audit.mapper.AuditCompanyMapper;
import com.job.admin.audit.mapper.AuditPostsMapper;
import com.job.admin.model.audit.pojo.AuditCompany;
import com.job.admin.model.audit.pojo.AuditPosts;
import com.job.admin.model.client.company.dto.CompanyAuth;
import com.job.admin.model.rabbitmq.AuditConstant;
import com.job.admin.model.audit.pojo.AuditAvatar;
import com.job.client.common.exception.MyselfException;
import com.job.client.common.utils.DateTimeUtils;
import com.job.client.model.posts.pojo.Posts;
import org.bouncycastle.pqc.crypto.newhope.NHSecretKeyProcessor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class RabbitMqListener {
    @Autowired
    private AuditAvatarMapper auditAvatarMapper;
    @Autowired
    private AuditCompanyMapper auditCompanyMapper;
    @Autowired
    private AuditPostsMapper auditPostsMapper;

    /*
    * 监听头像队列
    * */
    @RabbitListener(queues = AuditConstant.QUEUE_AUDIT_AVATAR)
    public void avatarListener(String dataJSON){
        HashMap<String,String> data = JSON.parseObject(dataJSON, HashMap.class);
        String userId = data.get("userId");
        String avatarImage = data.get("avatarImage");
        if(data==null || userId==null || avatarImage==null){
            new MyselfException(201,"头像审核队列接收到的数据为空",null);
            return;
        }
        AuditAvatar auditAvatar = new AuditAvatar(userId,avatarImage, DateTimeUtils.createNowTime());
        int i = auditAvatarMapper.insert(auditAvatar);
        if(i!=1){
            new MyselfException(201,"mp保存头像审核数据的时候报错",null);
            return;
        }
    }
    /*监听公司队列*/
    @RabbitListener(queues = AuditConstant.QUEUE_AUDIT_COMPANY)
    public void companyListener(String dataJSON){
        CompanyAuth companyAuth = JSON.parseObject(dataJSON, CompanyAuth.class);
        if(companyAuth == null){
            new MyselfException(201,"公司审核队列接收到的数据为空",null);
            return;
        }
        AuditCompany auditCompany = new AuditCompany(
                companyAuth.getCompanyName(),
                companyAuth.getUserId(),
                companyAuth.getBankAccount(),
                companyAuth.getBankStyle(),
                DateTimeUtils.createNowTime()
        );
        int i = auditCompanyMapper.insert(auditCompany);
        if(i!=1){
            new MyselfException(201,"mp保存公司审核数据的时候报错",null);
            return;
        }
    }
    /*
    *  监听职位队列
    * */
    @RabbitListener(queues = AuditConstant.QUEUE_AUDIT_POSTS)
    public void postsListener(String dataJSON){
        if(dataJSON == null){
            new MyselfException(201,"职位审核队列接收到的数据为空",null);
            return;
        }
        HashMap<String,String> data = JSON.parseObject(dataJSON, HashMap.class);
        String postsId = data.get("postsId");
        String userId = data.get("userId");
        AuditPosts auditPosts = new AuditPosts(postsId,userId,DateTimeUtils.createNowTime());
        int i = auditPostsMapper.insert(auditPosts);
        if(i!=1){
            new MyselfException(201,"mp保存职位审核数据的时候报错",null);
            return;
        }
    }
}
